﻿Imports System.Windows.Forms

Public Class frmGISOptions

    Private ColorScheme As New Dictionary(Of String, Color())

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub cbo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboColor0.SelectedIndexChanged, cboColor1.SelectedIndexChanged, cboColor2.SelectedIndexChanged, cboColor3.SelectedIndexChanged, cboColor4.SelectedIndexChanged
        ResetScheme()
        picColor.Refresh()
    End Sub

    Private Sub chkInvert_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkInvert.CheckedChanged
        lblColor1.Text = "Color &1 (" & IIf(chkInvert.Checked, "Max", "Min") & " Value):"
        lblColor4.Text = "Color &4 (" & IIf(chkInvert.Checked, "Min", "Max") & " Value):"
    End Sub

    Private Sub ResetScheme()
        cboColorScheme.SelectedIndex = -1
        'see if colors selected that match scheme
        For Each kv As KeyValuePair(Of String, Color()) In ColorScheme
            Dim AllMatch As Boolean = True
            For i As Integer = 1 To 4
                Dim cbo As WRDB.Controls.ColorPickerCombo = Choose(i, cboColor1, cboColor2, cboColor3, cboColor4)
                If cbo.Text <> kv.Value(i - 1).Name Then
                    AllMatch = False
                    Exit For
                End If
            Next
            If AllMatch Then
                cboColorScheme.Text = kv.Key
                Exit For
            End If
        Next
    End Sub

    Private Sub frmGISOptions_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ColorScheme.Add("High Contrast", New Color() {Color.Green, Color.Yellow, Color.Red, Color.White})
        ColorScheme.Add("Red", New Color() {Color.Pink, Color.LightPink, Color.Red, Color.DarkRed})
        ColorScheme.Add("Blue", New Color() {Color.LightBlue, Color.Blue, Color.MediumBlue, Color.DarkBlue})
        ColorScheme.Add("Green", New Color() {Color.LightGreen, Color.MediumSeaGreen, Color.Green, Color.DarkGreen})
        ColorScheme.Add("Orange", New Color() {Color.Bisque, Color.Orange, Color.DarkOrange, Color.Chocolate})
        ColorScheme.Add("Purple", New Color() {Color.PowderBlue, Color.MediumPurple, Color.Purple, Color.Indigo})
        ColorScheme.Add("Gray", New Color() {Color.WhiteSmoke, Color.LightGray, Color.DarkGray, Color.DimGray})
        ColorScheme.Add("Blue-Green", New Color() {Color.LightBlue, Color.DarkCyan, Color.Green, Color.DarkGreen})
        ColorScheme.Add("Purple-Red", New Color() {Color.Lavender, Color.MediumPurple, Color.Magenta, Color.Red})
        ColorScheme.Add("Yellow-Brown", New Color() {Color.LightYellow, Color.SandyBrown, Color.Peru, Color.Brown})

        For Each s As String In ColorScheme.Keys
            cboColorScheme.Items.Add(s)
        Next

        chkThresholdMin.Checked = txtThresholdMin.Text > Integer.MinValue
        chkThresholdMax.Checked = txtThresholdMax.Text < Integer.MaxValue

        ResetScheme()
        picColor.Refresh()
    End Sub

    Private Sub picColor_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picColor.Paint
        Dim colors() As Color = {Color.FromName(Me.cboColor0.Text),
                                 Color.FromName(Me.cboColor1.Text),
                                 Color.FromName(Me.cboColor2.Text),
                                 Color.FromName(Me.cboColor3.Text),
                                 Color.FromName(Me.cboColor4.Text)}
        For i As Integer = 0 To cboColor0.Height - 1
            Dim thiscolor As Color = colors(0)
            e.Graphics.DrawLine(New Pen(thiscolor, 1), 0, i, picColor.Width - 1, i)
        Next
        For i As Integer = cboColor0.Height To picColor.Height - 1
            Dim thiscolor As Color = frmGIS.GradientColor(CSng(i - cboColor0.Height) / (picColor.Height - cboColor0.Height), colors, False, Double.MinValue, Double.MaxValue)
            e.Graphics.DrawLine(New Pen(thiscolor, 1), 0, i, picColor.Width - 1, i)
        Next
    End Sub

    Private Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        cboColor0.Text = Color.LightGray.Name
        cboColor1.Text = Color.Green.Name
        cboColor2.Text = Color.Yellow.Name
        cboColor3.Text = Color.Red.Name
        cboColor4.Text = Color.White.Name
        chkInvert.Checked = False
        chkThresholdMin.Checked = False
        chkThresholdMax.Checked = False
        txtThresholdMin.Text = ""
        txtThresholdMax.Text = ""
    End Sub

    Private Sub cboColorScheme_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cboColorScheme.SelectedValueChanged, cboScalingOption.SelectedValueChanged
        If cboColorScheme.SelectedIndex = -1 Then Exit Sub
        With ColorScheme(cboColorScheme.Text)
            For i As Integer = 1 To 4
                Dim cbo As WRDB.Controls.ColorPickerCombo = Choose(i, cboColor1, cboColor2, cboColor3, cboColor4)
                cbo.Text = .GetValue(i - 1).name
            Next
        End With
    End Sub

    Private Sub chkThresholdMin_CheckedChanged(sender As Object, e As EventArgs) Handles chkThresholdMin.CheckedChanged
        txtThresholdMin.Visible = chkThresholdMin.Checked
        If Not chkThresholdMin.Checked Then txtThresholdMin.Text = Integer.MinValue Else txtThresholdMin.Text = "0"
    End Sub

    Private Sub chkThresholdMax_CheckedChanged(sender As Object, e As EventArgs) Handles chkThresholdMax.CheckedChanged
        txtThresholdMax.Visible = chkThresholdMax.Checked
        If Not chkThresholdMax.Checked Then txtThresholdMax.Text = Integer.MaxValue Else txtThresholdMax.Text = "100"
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        cboScalingOption.Items.Add("Use entire range of data for selected parameter")
        cboScalingOption.Items.Add("Use range of data for selected parameter statistic")

    End Sub

    Private Sub chkOverrideScheme_CheckedChanged(sender As Object, e As EventArgs) Handles chkOverrideScheme.CheckedChanged
        tblAutoScheme.Enabled = Not chkOverrideScheme.Checked
    End Sub
End Class
